package com.ymatou.authapp.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.authapp.parameter.UserBillsRequestDtoBean;
import com.ymatou.authapp.service.UserBillsRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.mongo.LoginIdUpdateEventHistoryMongoWapper;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
*用户凭据更新请求接口
*FreeMaker Template自动生成代码
*这个接口，现在不修改登录密码，交易密码，修改密码走 PasswordRequestDto 接口
*/
@TestSuite
public class Ts_UserBillsRequestDto{
	private static UserBillsRequestDtoBean userbillsrequestdtoBean;
	private static UserBillsRequestDtoCall userbillsrequestdtoCall;
	private static ULC201406Wapper ulcWapper;
	private static String loginId;
	private static String email;
	private static String mobile;
	private static String tradingPas;
	private static String userPas;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户凭据更新请求接口");
	}

	@Before
	public void caseUp() throws Exception {
		userbillsrequestdtoBean=new UserBillsRequestDtoBean();
		userbillsrequestdtoCall=new UserBillsRequestDtoCall();
		ulcWapper = new ULC201406Wapper();
	}

	@AfterClass
	public static void tearDown() throws Throwable {
		Logger.generateResult(userbillsrequestdtoCall.getOpurl());
		ulcWapper.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserBillsRequestDto_001() {
		Logger.start(true,"更新用户凭证成功(手机，邮箱，用户登陆Id)");
		try {
			initUserPwdTradingPwd();
			userbillsrequestdtoBean.setUserId(107);
			userbillsrequestdtoBean.setLoginPassword(userPas);
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail(email);
			userbillsrequestdtoBean.setLoginId(loginId);
			userbillsrequestdtoBean.setMobile(mobile);
			userbillsrequestdtoBean.setTradingPassword(tradingPas);
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(false);
			userbillsrequestdtoBean.setActionSource("MainSite");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();			
			
			ULC201406Bean user = ulcWapper.getULCInfoByUid(107);
			Logger.verifyEquals(true, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("OK", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(loginId.toLowerCase(), user.getlBills().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(email.toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(mobile, user.getlBills().getMobile(), "验证手机");
			Logger.verifyEquals(false, user.isLocked(), "验证锁定状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserBillsRequestDto_002() {
		Logger.start(false,"不输入任何值，失败");
		try {
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();			
			Logger.verifyEquals(false, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("LoingIdOrPasError", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(ECode.AUTH_NOTFOUND_USER, userbillsrequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserBillsRequestDto_003() {
		Logger.start(true,"输入默认的新的用户密码，失败");
		try {
			initUserPwdTradingPwd();
			userbillsrequestdtoBean.setUserId(107);
			userbillsrequestdtoBean.setLoginPassword("_NULLYMATOU_");
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail(email);
			userbillsrequestdtoBean.setLoginId(loginId);
			userbillsrequestdtoBean.setMobile(mobile);
			userbillsrequestdtoBean.setTradingPassword(tradingPas);
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(false);
			userbillsrequestdtoBean.setActionSource("MainSite");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();	
			
			ULC201406Bean user = ulcWapper.getULCInfoByUid(107);
			Logger.verifyEquals(true, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("OK", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(loginId.toLowerCase(), user.getlBills().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(email.toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(mobile, user.getlBills().getMobile(), "验证手机");
			Logger.verifyEquals(false, user.isLocked(), "验证锁定状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserBillsRequestDto_004() {
		Logger.start(true,"输入空的新的交易密码，则交易密码不会被修改");
		try {
			initUserPwdTradingPwd();
			userbillsrequestdtoBean.setUserId(107);
			userbillsrequestdtoBean.setLoginPassword(userPas);
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail(email);
			userbillsrequestdtoBean.setLoginId(loginId);
			userbillsrequestdtoBean.setMobile(mobile);
			userbillsrequestdtoBean.setTradingPassword("");
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(true);
			userbillsrequestdtoBean.setActionSource("MainSite");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();	
			
			ULC201406Bean user = ulcWapper.getULCInfoByUid(107);
			Logger.verifyEquals(true, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("OK", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(loginId.toLowerCase(), user.getlBills().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(email.toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(mobile, user.getlBills().getMobile(), "验证手机");
			Logger.verifyEquals(true, user.isLocked(), "验证锁定状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserBillsRequestDto_005() {
		Logger.start(true,"输入默认的新的交易密码，失败");
		try {
			initUserPwdTradingPwd();
			userbillsrequestdtoBean.setUserId(107);
			userbillsrequestdtoBean.setLoginPassword(userPas);
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail(email);
			userbillsrequestdtoBean.setLoginId(loginId);
			userbillsrequestdtoBean.setMobile(mobile);
			userbillsrequestdtoBean.setTradingPassword("_NULLYMATOU_");
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(false);
			userbillsrequestdtoBean.setActionSource("MainSite");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();	
			
			ULC201406Bean user = ulcWapper.getULCInfoByUid(107);
			Logger.verifyEquals(true, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("OK", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(loginId.toLowerCase(), user.getlBills().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(email.toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(mobile, user.getlBills().getMobile(), "验证手机");
			Logger.verifyEquals(false, user.isLocked(), "验证锁定状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserBillsRequestDto_006() {
		Logger.start(true,"如果用户是App注册用户，验证用户认证信息同步更新");
		try {
			initUserPwdTradingPwd();
			userbillsrequestdtoBean.setUserId(107);
			userbillsrequestdtoBean.setLoginPassword(userPas);
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail(email);
			userbillsrequestdtoBean.setLoginId(loginId);
			userbillsrequestdtoBean.setMobile(mobile);
			userbillsrequestdtoBean.setTradingPassword(tradingPas);
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(false);
			userbillsrequestdtoBean.setActionSource("App");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();			
			
			ULC201406Bean user = ulcWapper.getULCInfoByUid(107);
			Logger.verifyEquals(true, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("OK", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(loginId.toLowerCase(), user.getlBills().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(email.toLowerCase(), user.getlBills().getEmail(), "验证邮箱");
			Logger.verifyEquals(mobile, user.getlBills().getMobile(), "验证手机");
			Logger.verifyEquals(false, user.isLocked(), "验证锁定状态");
			
			Thread.sleep(3000);
			LoginIdUpdateEventHistoryMongoWapper wapper = new LoginIdUpdateEventHistoryMongoWapper();
			Map<String, Object> result = wapper.getModifyLoginIdInfo(loginId, 1);
			Logger.verifyNotNull(result, "App用户认证信息同步");
			Logger.verifyEquals(loginId, result.get("nId"), "验证登陆ID已同步");
			Logger.verifyEquals(107, result.get("uId"), "验证用户ID已同步");
			Logger.verifyEquals(mobile, result.get("mobile"), "验证手机已同步");
			Logger.verifyEquals(1, result.get("uSource"), "验证用户注册入口已同步");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserBillsRequestDto_007() {
		Logger.start(true,"输入已存在的手机号，邮箱，登陆Id，则手机号，邮箱，登陆Id不会被修改");
		try {
			initUserPwdTradingPwd();
			ULC201406Bean originalUser = ulcWapper.getULCInfoByUid(107);
			userbillsrequestdtoBean.setUserId(107);
			userbillsrequestdtoBean.setLoginPassword(userPas);
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail("kitty270@sohu.com");
			userbillsrequestdtoBean.setLoginId("kexin");
			userbillsrequestdtoBean.setMobile("13654197476");
			userbillsrequestdtoBean.setTradingPassword(tradingPas);
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(false);
			userbillsrequestdtoBean.setActionSource("MainSite");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();	
			
			ULC201406Bean user = ulcWapper.getULCInfoByUid(107);
			Logger.verifyEquals(true, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("OK", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(originalUser.getlBills().getLoginId(), user.getlBills().getLoginId(), "验证登陆ID");
			Logger.verifyEquals(originalUser.getlBills().getEmail(), user.getlBills().getEmail(), "验证邮箱");
			//手机这边的验证暂时被注释掉了	
//			Logger.verifyEquals(originalUser.getlBills().getMobile(), user.getlBills().getMobile(), "验证手机");
			Logger.verifyEquals(false, user.isLocked(), "验证锁定状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserBillsRequestDto_008() {
		Logger.start(false,"输入不存在的用户ID，失败");
		try {
			initUserPwdTradingPwd();
			userbillsrequestdtoBean.setUserId(-48484);
			userbillsrequestdtoBean.setLoginPassword(userPas);
			userbillsrequestdtoBean.setOldLoginPassword("123456a");
			userbillsrequestdtoBean.setMail("kitty270@sohu.com");
			userbillsrequestdtoBean.setLoginId("kexin");
			userbillsrequestdtoBean.setMobile("13654197476");
			userbillsrequestdtoBean.setTradingPassword(tradingPas);
			userbillsrequestdtoBean.setOldTradingPassword("abc123");
			userbillsrequestdtoBean.setLocked(false);
			userbillsrequestdtoBean.setActionSource("MainSite");
			userbillsrequestdtoBean.setClientType("VIP");
			userbillsrequestdtoBean.setClientIp("120.32.4.1");
			userbillsrequestdtoCall.setData(userbillsrequestdtoBean);
			userbillsrequestdtoCall.callService();	
			
			Logger.verifyEquals(false, userbillsrequestdtoCall.getSuccess(), "验证请求是否成功");
			Logger.verifyEquals("LoingIdOrPasError", userbillsrequestdtoCall.getResult(), "获取操作结果");
			Logger.verifyEquals(ECode.AUTH_NOTFOUND_USER, userbillsrequestdtoCall.getMsg(), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 初始化一下密码
	 * @throws Exception 
	 */
	private void initUserPwdTradingPwd() throws Exception{
		loginId = TestDataManager.randomStr(6);
		email = TestDataManager.randomStr(10)+"@ymatou.com";
		mobile = TestDataManager.getRandomNumToHex(11);
		tradingPas = TestDataManager.getRandomNum(7);
		userPas = TestDataManager.getRandomNum(6);
		
		Logger.comment("初始化用户密码为123456，交易密码为abc123");
		//由于密码加过密，重新初始化一下密码
		ulcWapper.updatePwdByUid(107);
		ulcWapper.updatetPasByUid(107);
		YmtUsersWapper userWapper = new YmtUsersWapper();
		userWapper.updateTradingPwdByUid(107);
	}
}